#!/usr/bin/env python 3.6
# -*- coding: utf-8 -*-
# @Time      :2019/4/8
# @Author    :Tian Maohong
# @Email     :tianmaohong@bbdservice.com
# @File      :R34_EC_UpaidCreditInformationSummaryCheck_2D_XD
# @Software  :PyCharm

'''eof
name:申请企业未结清信贷业务为资产处置、垫款汇总的笔数校验
code:R34_EC_UpaidCreditInformationSummaryCheck_2D_XD
tableName:
columnName:
groups:企业征信校验
dependencies:EC_DSJ_2D
type:常用指标
datasourceType:在线指标
description:
eof'''


import re
import sys
import datetime


reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--', []]

def changeformatData(table_Name):
    """
    获取数据
    :param table_Name:字典keys
    :return:[{}]
    """
    try:
        data = EC_DSJ_2D["data"].get(table_Name)
        # table 不存在
        if data is None:
            return None
        # 表存在 但是为空[]
        elif data == "" or (isinstance(data, list) and len(data) == 0):
            return [{}]
        else:
            return data
    except:
        return None

def Formate(time):
    import re
    import datetime
    r = re.compile(r'\D+')
    try:
        date = datetime.datetime.strptime(time, '%Y.%m.%d').strftime('%Y%m%d')
    except:
        try:
            date = datetime.datetime.strptime(time, '%Y-%m-%d').strftime('%Y%m%d')
        except:
            try:
                date = datetime.datetime.strptime(time, '%Y/%m/%d').strftime('%Y%m%d')
            except:
                try:
                    tmp = r.sub('', time)
                    date = datetime.datetime.strptime(tmp, '%Y%m%d').strftime('%Y%m%d')
                except:
                    date = None
    return date

def formatData(table_Name):
    """
    获取数据
    :param table_Name:字典keys
    :return:[{}]
    """
    try:
        data = EC_DSJ_2D["data"].get(table_Name)
        # table 不存在
        if data is None:
            return None
        # 表存在 但是为空[]
        elif data == "" or (isinstance(data, list) and len(data) == 0):
            return [{}]
        else:
            return data
    except:
        return None

def isFloat(x):
    '''
    判断x为float类型且大于等于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x) >= 0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False


def convertDataType(data_value, data_type):
    """
    数据格式转换
    :param data_value:
    :param data_type: float/int/str/date_time
    :return:
    """

    return_data = None
    data_value = str(data_value)
    # float
    if data_type == 'float':
        try:
            return_data = float(data_value) if len(data_value) >= 0 else None
        except:
            return_data = None
    # int
    elif data_type == 'int':
        try:
            return_data = int(data_value) if len(data_value) >= 0 else None
        except:
            return_data = None
    # str
    elif data_type == 'str':
        try:
            return_data = str(data_value) if len(data_value) >= 0 else None
        except:
            return_data = None
    # date_time
    elif data_type == 'date_time':
        r = re.compile(r'\D+')
        try:
            return_data = datetime.datetime.strptime(data_value, '%Y.%m.%d').strftime('%Y-%m-%d')
        except:
            try:
                return_data = datetime.datetime.strptime(data_value, '%Y-%m-%d').strftime('%Y-%m-%d')
            except:
                try:
                    return_data = datetime.datetime.strptime(data_value, '%Y/%m/%d').strftime('%Y-%m-%d')
                except:
                    try:
                        data_value = r.sub('', data_value)
                        return_data = datetime.datetime.strptime(data_value, '%Y%m%d').strftime('%Y-%m-%d')
                    except:
                        return_data = None

    return return_data

def factor1(EBA):
    '''Factor1=统计信用提示信息单元·信贷交易提示信息段(EB01A)·被追偿的借贷交易余额（EB01AJ02）＞0的记录数；'''
    # Factor1
    if EBA in null_type_list or EBA == [{}]:
        Factor1 = None
    else:
        EB01 = EBA[0].get("EB01")
        if EB01 is None:
            Factor1 = None
        elif EB01 == "":
            Factor1 = 0
        else:
            EB01A = EB01[0].get("EB01A")
            if EB01A is None:
                Factor1 = 0
            elif EB01A in null_type_list:
                Factor1 = None
            else:
                EB01AJ02 = EB01A[0].get("EB01AJ02")
                if EB01AJ02 in null_type_list:
                    Factor1 = None
                else:
                    if isFloat(EB01AJ02):
                        if convertDataType(EB01AJ02, 'float') > 0:
                            Factor1 = 1
                        else:
                            Factor1 = 0
                    else:
                        Factor1 = None
    return Factor1

def factor2(EBB):
    '''Factor2=统计借贷交易汇总信息单元·未结清借贷交易汇总信息段(EB02A)·资产处置业务账户数(EB02AS01)
     or 资产处置业务余额(EB02AJ01)＞0的记录数；
'''
    # Factor2/Factor3
    if EBB in null_type_list:
        Factor2 = None
    elif EBB == [{}]:
        Factor2 = 0
    else:
        EB02 = EBB[0].get("EB02")
        if EB02 is None:
            Factor2 = 0
        elif EB02 in null_type_list:
            Factor2 = None
        else:
            EB02A = EB02[0].get("EB02A")
            if EB02A in null_type_list:
                Factor2 = None
            else:
                # 资产处置
                EB02AS01 = EB02A[0].get("EB02AS01")
                EB02AJ01 = EB02A[0].get("EB02AJ01")

                # 垫款业务
                # EB02AS02 = EB02A[0].get("EB02AS02")  # 垫款业务账户数
                # EB02AJ02 = EB02A[0].get("EB02AJ02")  # 垫款业务余额

                # 资产处置类统计
                if EB02AS01 in null_type_list or EB02AJ01 in null_type_list:
                    Factor2 = None
                else:
                    if isFloat(EB02AS01) and convertDataType(EB02AS01, 'float') > 0:
                        Factor2 = convertDataType(EB02AS01, 'float')
                    else:
                        if isFloat(EB02AJ01) and convertDataType(EB02AJ01, 'float') > 0:
                            Factor2 = 1
                        else:
                            Factor2 = 0
    return Factor2


def factor3(EBB):
    '''Factor3=统计借贷交易汇总信息单元·未结清借贷交易汇总信息段(EB02A)·垫款业务账户数(EB02AS02)or
    垫款业务余额(EB02AJ02)＞0的记录数；
 '''
    # Factor2/Factor3
    if EBB in null_type_list:
        Factor3 = None
    elif EBB == [{}]:
        Factor3 = 0
    else:
        EB02 = EBB[0].get("EB02")
        if EB02 is None:
            Factor3 = 0
        elif EB02 in null_type_list:
            Factor3 = None
        else:
            EB02A = EB02[0].get("EB02A")
            if EB02A in null_type_list:
                Factor3 = None
            else:
                # 资产处置
                # EB02AS01 = EB02A[0].get("EB02AS01")
                # EB02AJ01 = EB02A[0].get("EB02AJ01")

                # 垫款业务
                EB02AS02 = EB02A[0].get("EB02AS02")  # 垫款业务账户数
                EB02AJ02 = EB02A[0].get("EB02AJ02")  # 垫款业务余额

                # 垫款业务统计
                if EB02AS02 in null_type_list or EB02AJ02 in null_type_list:
                    Factor3 = None
                else:
                    if isFloat(EB02AS02) and convertDataType(EB02AS02, 'float') > 0:
                        Factor3 = convertDataType(EB02AS02, 'float')
                    else:
                        if isFloat(EB02AJ02) and convertDataType(EB02AJ02, 'float') > 0:
                            Factor3 = 1
                        else:
                            Factor3 = 0
    return Factor3



def factor4(EDA):
    '''Factor4=统计借贷账户信息单元·基本信息段(ED01A)·账户活动状态(ED01AD01)==1(未结清)
    and 借贷账户类型(ED01AD02) ∈(D1(非循环贷账户),D2(贴现账户),R1(循环贷账户),R4(循环额度下分账户),C1(催收账户))
    and借贷业务种类大类(ED01AD05)in(41(垫款),51(资产处置)）的笔数;
'''
    ED01AD02List = ['D1', 'D2', 'R1', 'R4', 'C1']  # D1(非循环贷账户),D2(贴现账户),R1(循环贷账户),R4(循环额度下分账户),C1(催收账户)
    ED01AD05List = ['41', '51']  # 借贷业务种类大类(ED01AD05)in(41(垫款),51(资产处置)

    if EDA is None:
        Factor4 = None
    elif EDA == [{}]:
        Factor4 = 0
    else:
        ED01 = EDA[0].get("ED01")
        if ED01 is None:
            Factor4 = 0
        elif ED01 in null_type_list:
            Factor4 = None
        else:
            result4 = []
            for i in range(len(ED01)):
                ED01A = ED01[i].get("ED01A")

                if ED01A in null_type_list:
                    result4.append(None)
                else:
                    ED01AD01 = ED01A[0].get("ED01AD01")
                    ED01AD02 = ED01A[0].get("ED01AD02")
                    ED01AD05 = ED01A[0].get("ED01AD05")

                    if ED01AD01 in null_type_list or ED01AD02 in null_type_list or ED01AD05 in null_type_list:
                        result4.append(None)
                    else:
                        # Factor4
                        if convertDataType(ED01AD01, 'str') == '1' and \
                                convertDataType(ED01AD02, 'str') in ED01AD02List and \
                                convertDataType(ED01AD05, 'str') in ED01AD05List:
                            result4.append(1)
                        else:
                            result4.append(0)

            result4 = filter(lambda x:x is not None, result4)

            if len(result4) == 0:
                Factor4 = None
            else:
                Factor4 = sum(result4)
    return Factor4

def factor5(EDA):
    '''    Factor5=统计借贷账户信息单元下基本信息段（ED01A）·账户活动状态（ED01AD01）==1(未结清)
    and借贷账户类型(ED01AD02)==C1(催收账户)的笔数；
'''
    if EDA is None:
        Factor5 = None
    elif EDA == [{}]:
        Factor5 = 0
    else:
        ED01 = EDA[0].get("ED01")
        if ED01 is None:
            Factor5 = 0
        elif ED01 in null_type_list:
            Factor5 = None
        else:
            result5 = []
            for i in range(len(ED01)):
                ED01A = ED01[i].get("ED01A")

                if ED01A in null_type_list:
                    result5.append(None)
                else:
                    ED01AD01 = ED01A[0].get("ED01AD01")
                    ED01AD02 = ED01A[0].get("ED01AD02")

                    if ED01AD01 in null_type_list or ED01AD02 in null_type_list:
                        result5.append(None)
                    else:
                        # Factor5
                        if convertDataType(ED01AD01, 'str') == '1' and \
                                convertDataType(ED01AD02, 'str') == 'C1':
                            # Factor5 = Factor5 + 1
                            result5.append(1)
                        else:
                            result5.append(0)

            result5 = filter(lambda x:x is not None, result5)

            if len(result5) == 0:
                Factor5 = None
            else:
                Factor5 = sum(result5)
    return Factor5


def factor6(EDA):
    '''    Factor6=统计借贷账户信息单元下基本信息段（ED01A）·账户活动状态（ED01AD01）==1(未结清)
     且借贷账户类型(ED01AD02) ∈(D1(非循环贷账户),D2(贴现账户),R1(循环贷账户),R4(循环额度下分账户))
     且还款表现信息段(ED01B)·
     最近一次还款形式（ED01BD02）∈(21(担保代偿),43(资产剥离),51(债转股),31(资产重组),32(减免),41(债权转让),42(债券置换),52(以资抵债),61(诉讼追偿),62(破产清偿),63(委托处置))的笔数；
'''
    ED01AD02List1 = ['D1', 'D2', 'R1', 'R4']  # D1(非循环贷账户), D2(贴现账户), R1(循环贷账户), R4(循环额度下分账户)
    # 最近一次还款形式（ED01BD02）∈(21(担保代偿), 43(资产剥离), 51(债转股), 31(资产重组), 32(减免),
    # 41(债权转让), 42(债券置换),52(以资抵债), 61(诉讼追偿), 62(破产清偿), 63(委托处置))的笔数
    ED01BD02List = ['21', '43', '51', '31', '32', '41', '42', '52', '61', '62', '63']

    if EDA is None:
        Factor6 = None
    elif EDA == [{}]:
        Factor6 = 0
    else:

        ED01 = EDA[0].get("ED01")
        if ED01 is None:
            Factor6 = 0
        elif ED01 in null_type_list:
            Factor6 = None
        else:
            result6 = []
            for i in range(len(ED01)):
                ED01A = ED01[i].get("ED01A")
                ED01B = ED01[i].get("ED01B")

                if ED01A in null_type_list or ED01B in null_type_list:
                    result6.append(None)
                else:
                    ED01AD01 = ED01A[0].get("ED01AD01") #账户活动状态ED01AD01
                    ED01AD02 = ED01A[0].get("ED01AD02") #借贷账户类型(ED01AD02)
                    # 处理FACTOR6
                    if ED01B in null_type_list:
                        result6.append(None)
                    else:
                        # Factor6
                        ED01BH = ED01B[0].get("ED01BH")
                        if ED01BH is None:
                            result6.append(0)
                        elif ED01BH in null_type_list:
                            result6.append(None)
                        else:
                            tmp = []
                            for i in range(len(ED01BH)):
                                ED01BD02 = ED01BH[i].get("ED01BD02")
                                if ED01BD02 in null_type_list or ED01AD01 in null_type_list or ED01AD02 in null_type_list:
                                    tmp.append(None)
                                else:
                                    if convertDataType(ED01AD01, 'str') == '1' and \
                                            convertDataType(ED01AD02, 'str') in ED01AD02List1 and \
                                            convertDataType(ED01BD02, 'str') in ED01BD02List:
                                        # Factor6 = Factor6 + 1
                                        tmp.append(1)
                                    else:
                                        tmp.append(0)
                            tmp = filter(lambda x:x is not None, tmp)
                            if len(tmp) == 0:
                                result6.append(None)
                            else:
                                result6.append(sum(tmp))

            result6 = filter(lambda x:x is not None, result6)
            if len(result6) == 0:
                Factor6 = None
            else:
                Factor6 = sum(result6)
    return Factor6

def factor7(EDA):
    '''    Factor7=统计借贷账户信息单元·特定交易信息段(ED01CH)·交易类型(ED01CD01)==11（展期）的笔数；
	Factor7=统计借贷账户信息单元·账户活动状态(ED01AD01)=1 且 特定交易信息段(ED01CH)·交易类型(ED01CD01)==11（展期）的笔数；其中不统计交易日期（ED01CR01）在2020年2月1日至2020年9月30日期间的数据。
	'''

    # Factor7
    if EDA is None:
        Factor7 = None
    elif EDA == [{}]:
        Factor7 = 0
    else:
        ED01 = EDA[0].get("ED01")
        if ED01 is None:
            Factor7 = 0
        elif ED01 in null_type_list:
            Factor7 = None
        else:
            result = []
            for k in range(len(ED01)):
                ED01A = ED01[k].get("ED01A")
                ED01C = ED01[k].get("ED01C")  # 特定交易信息段
                if ED01C is None or ED01A is None:
                    result.append(0)
                elif ED01C in null_type_list or ED01A in null_type_list:
                    result.append(None)
                else:
                    ED01AD01 = ED01A[0].get("ED01AD01") #账户活动状态ED01AD01
                    ED01CH = ED01C[0].get('ED01CH')
                    if ED01CH is None:
                        result.append(0)
                    elif ED01CH in null_type_list:
                        result.append(None)
                    else:
                        tmp = []
                        for i in range(len(ED01CH)):
                            ED01CD01 = ED01CH[i].get("ED01CD01")
                            ED01CR01 = ED01CH[i].get("ED01CR01")
                            if ED01CD01 in null_type_list or ED01AD01 in null_type_list:
                                tmp.append(None)
                            elif ED01CR01 in null_type_list:
                                # 交易类型为展期11
                                 if convertDataType(ED01AD01, 'str') == '1' and convertDataType(ED01CD01, 'str') == '11':
                                    tmp.append(1)
                                 else:
                                    tmp.append(0)
                            elif ED01CR01 not in null_type_list:
                                #times = int(''.join(ED01CR01.split('-')))
                                try:
                                    times = int(Formate(ED01CR01))
                                except:
                                    times = 0
                                # 交易类型为展期11
                                #if convertDataType(ED01AD01, 'str') == '1' and convertDataType(ED01CD01, 'str') == '11' and ED01CR01[:7] not in ['2020-02','2020-03','2020-04','2020-05','2020-06','2020-07','2020-08','2020-09','2020-10','2020-11','2020-12','2021-01','2021-02','2021-03','2021-04','2021-05','2021-06','2021-07','2021-08','2021-09','2021-10','2021-11','2021-12']:
                                if convertDataType(ED01AD01, 'str') == '1' and convertDataType(ED01CD01, 'str') == '11' and (times < 20200125 or times > 20211231):
                                    tmp.append(1)
                                else:
                                    tmp.append(0)
                            else:
                                tmp.append(0)
                        tmp = filter(lambda x: x is not None, tmp)
                        if len(tmp) == 0:
                            result.append(None)
                        else:
                            result.append(sum(tmp))
            result = filter(lambda x: x is not None, result)
            if len(result) == 0:
                Factor7 = None
            else:
                Factor7 = sum(result)
    return Factor7

def factor8(EDB):
    '''    Factor8=统计担保账户信息单元·在保责任信息段(ED04B)·账户活动状态(ED04BD01)==1(未结清)
    and代偿（垫款）标志(ED04BD03)==1(是)的笔数；
    '''
    if EDB is None:
        Factor8 = None
    elif EDB == [{}]:
        Factor8 = 0
    else:
        # Factor8
        ED04 = EDB[0].get("ED04")
        if ED04 is None:
            Factor8 = 0
        elif ED04 in null_type_list:
            Factor8 = None
        else:
            tmp = []
            for i in range(len(ED04)):
                ED04B = ED04[i].get("ED04B")
                if ED04B in null_type_list:
                    tmp.append(None)
                else:
                    ED04BD01 = ED04B[0].get("ED04BD01")
                    ED04BD03 = ED04B[0].get("ED04BD03")
                    if ED04BD01 in null_type_list or ED04BD03 in null_type_list:
                        tmp.append(None)
                    else:
                        if convertDataType(ED04BD01, 'str') == '1' and \
                                        convertDataType(ED04BD03, 'str') == '1':
                            tmp.append(1)
                        else:
                            tmp.append(0)
            tmp = filter(lambda x: x is not None, tmp)
            if len(tmp) == 0:
                Factor8 = None
            else:
                Factor8 = sum(tmp)
    return Factor8

def factor9(EDB):
    '''Factor9=统计担保账户分机构汇总信息单元(ED05)· 担保交易业务种类细分(ED050D02)∈(61(银行承兑汇票),05(信用证担保),71（融资类银行保函),72(非融资类银行保函))
     and 余额 （ED050J01）>0 and 垫款标志(ED050D04)==1(是)的笔数；；
'''
    ED050D02List = ['61', '05', '71', '72']  # 担保交易业务种类细分(ED050D02)∈(61(银行承兑汇票),05(信用证担保),71（融资类银行保函),72(非融资类银行保函))
    # Factor9
    if EDB is None:
        return None
    elif EDB == [{}]:
        return 0
    else:
        ED05 = EDB[0].get("ED05")
        if ED05 is None:
            return 0
        elif ED05 in null_type_list:
            return None
        else:
            tmp = []
            for i in range(len(ED05)):
                ED050D02 = ED05[i].get("ED050D02")
                ED050D04 = ED05[i].get("ED050D04")
                ED050J01 = ED05[i].get("ED050J01")
                if ED050D02 in null_type_list or ED050D04 in null_type_list or ED050J01 in null_type_list:
                    tmp.append(None)
                else:
                    if convertDataType(ED050D02, 'str') in ED050D02List and \
                                    convertDataType(ED050D04, 'str') == '1' and \
                            convertDataType(ED050J01, 'float') > 0:
                        tmp.append(1)
                    else:
                        tmp.append(0)
            tmp = filter(lambda x: x is not None, tmp)
            if len(tmp) == 0:
                return None
            else:
                return sum(tmp)


def factor10(EBE):
    '''Factor10=统计相关还款责任汇总信息单元·借贷交易相关还款责任汇总信息段(EB05A)·责任类型（EB05AD01）in(1(个人借贷交易共同还款人|“共同债务人),9(其他)) ·(被追偿账户的还款责任金额(EB05AJ01)>0
    or 被追偿账户数(EB05AS01)>0 or 被追偿账户余额(EB05AJ02)>0) 的笔数；
'''
    EB05AD01List = ['1', '9']

    if EBE is None:
        Factor10 = None
    elif EBE == [{}]:
        Factor10 = 0
    else:
        EB05 = EBE[0].get("EB05")
        if EB05 is None or EB05 == "":
            Factor10 = 0
        elif EB05 in null_type_list:
            Factor10 = None
        else:
            EB05A = EB05[0].get("EB05A")
            if EB05A is None or EB05A == "":
                Factor10 = 0
            elif EB05A in null_type_list:
                Factor10 = None
            else:
                EB05AH = EB05A[0].get("EB05AH")
                if EB05AH is None:
                    Factor10 = 0
                elif EB05AH in null_type_list:
                    Factor10 = None
                else:
                    tmp = []
                    for i in range(len(EB05AH)):
                        EB05AD01 = EB05AH[i].get("EB05AD01")
                        EB05AJ01 = EB05AH[i].get("EB05AJ01")
                        EB05AS02 = EB05AH[i].get("EB05AS02")
                        EB05AJ02 = EB05AH[i].get("EB05AJ02")

                        if EB05AD01 in null_type_list or (EB05AJ01 in null_type_list and
                                EB05AS02 in null_type_list and EB05AJ02 in null_type_list):
                            tmp.append(None)
                        else:
                            if convertDataType(EB05AD01, 'str') in EB05AD01List:
                                if isFloat(EB05AJ01) and convertDataType(EB05AJ01, 'float') > 0:
                                    tmp.append(1)
                                    continue

                                if isFloat(EB05AS02) and convertDataType(EB05AS02, 'float') > 0:
                                    tmp.append(1)
                                    continue

                                if isFloat(EB05AJ02) and convertDataType(EB05AJ02, 'float') > 0:
                                    tmp.append(1)
                                    continue
                                tmp.append(0)

                            else:
                                tmp.append(0)
                    tmp = filter(lambda x:x is not None,tmp)
                    if len(tmp) == 0:
                        Factor10 = None
                    else:
                        Factor10 = sum(tmp)
    return Factor10


def R34_EC_UpaidCreditInformationSummaryCheck():
    """
    Factor1=统计信用提示信息单元·信贷交易提示信息段(EB01A)·被追偿的借贷交易余额（EB01AJ02）＞0的记录数；
    Factor2=统计借贷交易汇总信息单元·未结清借贷交易汇总信息段(EB02A)·资产处置业务账户数(EB02AS01) or 资产处置业务余额(EB02AJ01)＞0的记录数；
    Factor3=统计借贷交易汇总信息单元·未结清借贷交易汇总信息段(EB02A)·垫款业务账户数(EB02AS02)or垫款业务余额(EB02AJ02)＞0的记录数；
    Factor4=统计借贷账户信息单元·基本信息段(ED01A)·账户活动状态(ED01AD01)==1(未结清) and 借贷账户类型(ED01AD02) ∈(D1(非循环贷账户),D2(贴现账户),R1(循环贷账户),R4(循环额度下分账户),C1(催收账户))and借贷业务种类大类(ED01AD05)in(41(垫款),51(资产处置)）的笔数;
    Factor5=统计借贷账户信息单元下基本信息段（ED01A）·账户活动状态（ED01AD01）==1(未结清) and借贷账户类型(ED01AD02)==C1(催收账户)的笔数；
    Factor6=统计借贷账户信息单元下基本信息段（ED01A）·账户活动状态（ED01AD01）==1(未结清) 且借贷账户类型(ED01AD02) ∈(D1(非循环贷账户),D2(贴现账户),R1(循环贷账户),R4(循环额度下分账户))且还款表现信息段(ED01B)·最近一次还款形式（ED01BD02）∈(21(担保代偿),43(资产剥离),51(债转股),31(资产重组),32(减免),41(债权转让),42(债券置换),52(以资抵债),61(诉讼追偿),62(破产清偿),63(委托处置))的笔数；
    Factor7=统计借贷账户信息单元·特定交易信息段(ED01CH)·交易类型(ED01CD01)==11（展期）的笔数；
    Factor8=统计担保账户信息单元·在保责任信息段(ED04B)·账户活动状态(ED04BD01)==1(未结清)and代偿（垫款）标志(ED04BD03)==1(是)的笔数；
    Factor9=统计担保账户分机构汇总信息单元(ED05)· 担保交易业务种类细分(ED04AD03)∈(61(银行承兑汇票),05(信用证担保),71（融资类银行保函),72(非融资类银行保函)) and 垫款标志(ED050D04)==1(是)的笔数；
    Factor10=统计相关还款责任汇总信息单元·借贷交易相关还款责任汇总信息段(EB05A)·责任类型（EB05AD01）in(1(个人借贷交易共同还款人|“共同债务人),9(其他)) ·(被追偿账户的还款责任金额(EB05AJ01)>0 or 被追偿账户数(EB05AS01)>0 or 被追偿账户余额(EB05AJ02)>0) 的笔数；
    Factor=Factor1+Factor2+Factor3+Factor4+Factor5+Factor6+Factor7+Factor8+Factor9+Factor10。

    :return:
    """
    Null = ['', ' ', None, 'nulll', 'Null', 'NULL', '/', '-', '--']
    ED01AD02List = ['D1', 'D2', 'R1', 'R4', 'C1']  # D1(非循环贷账户),D2(贴现账户),R1(循环贷账户),R4(循环额度下分账户),C1(催收账户)
    ED01AD05List = ['41', '51']  # 借贷业务种类大类(ED01AD05)in(41(垫款),51(资产处置)
    ED01AD02List1 = ['D1', 'D2', 'R1', 'R4']  # D1(非循环贷账户), D2(贴现账户), R1(循环贷账户), R4(循环额度下分账户)
    # 最近一次还款形式（ED01BD02）∈(21(担保代偿), 43(资产剥离), 51(债转股), 31(资产重组), 32(减免),
    # 41(债权转让), 42(债券置换),52(以资抵债), 61(诉讼追偿), 62(破产清偿), 63(委托处置))的笔数
    ED01BD02List = ['21', '43', '51', '31', '32', '41', '42', '52', '61', '62', '63']
    ED050D02List = ['61', '05', '71', '72']  # 担保交易业务种类细分(ED050D02)∈(61(银行承兑汇票),05(信用证担保),71（融资类银行保函),72(非融资类银行保函))
    EB05AD01List = ['1', '9']  # 责任类型（EB05AD01）in(1(个人借贷交易共同还款人|“共同债务人),9(其他))

    EBA = formatData("EBA")  # 信用提示信息
    EBB = formatData("EBB")  # 借贷交易汇总信息
    EDA = formatData("EDA")  # 借贷账户信息
    EDB = formatData("EDB")  # 担保账户信息
    EBE = formatData("EBE")  # 相关还款责任汇总信息

    Factor1 = factor1(EBA)
    Factor2 = factor2(EBB)
    Factor3 = factor3(EBB)
    Factor4 = factor4(EDA)
    Factor5 = factor5(EDA)
    Factor6 = factor6(EDA)
    Factor7 = factor7(EDA)
    Factor8 = factor8(EDB)
    Factor9 = factor9(EDB)
    Factor10 = factor10(EBE)

    if Factor1 is None and Factor2 is None and Factor3 is None and Factor4 is None and Factor5 is None \
            and Factor6 is None and Factor7 is None and Factor8 is None and Factor9 is None and Factor10 is None:
        return u'缺失值'
    else:
        if Factor1 is None:
            Factor1 = 0
        if Factor2 is None:
            Factor2 = 0
        if Factor3 is None:
            Factor3 = 0
        if Factor4 is None:
            Factor4 = 0
        if Factor5 is None:
            Factor5 = 0
        if Factor6 is None:
            Factor6 = 0
        if Factor7 is None:
            Factor7 = 0
        if Factor8 is None:
            Factor8 = 0
        if Factor9 is None:
            Factor9 = 0
        if Factor10 is None:
            Factor10 = 0
        Factor = Factor10 + Factor9 + Factor8 + Factor7 + Factor6 + Factor5 + Factor4 + Factor3 + Factor2 + Factor1
        return Factor




result = R34_EC_UpaidCreditInformationSummaryCheck()
# if __name__ == '__main__':
# import json
# with open(r'C:\Users\john\Desktop\samples\data(1).json') as ff:
#     dd = json.loads(ff.read())
# EC_DSJ_2D = dd['EC_DSJ_2D']
# result = R34_EC_UpaidCreditInformationSummaryCheck()
# print(result)